package com.flyee.viewer
{
	import com.flyee.controller.UIController;
	import com.flyee.controller.tools.sandtable.EstateService;
	import com.flyee.controller.tools.sandtable.TitleOptionBar;
	import com.flyee.events.PanelStateChangeEvent;
	import com.flyee.service.pool.ObjectCollection;
	import com.flyee.utils.GenerateIDTool;
	import com.flyee.utils.depot.SandTableDepot;
	import com.flyee.utils.gs.TweenLite;
	import com.flyee.utils.gs.easing.Expo;
	
	import flash.display.Bitmap;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.effects.easing.Exponential;
	import mx.events.CloseEvent;
	import mx.events.FlexEvent;

	public class SandTableViewer extends BaseWindowViewer
	{
		public static var NAME:String = "com.flyee.viewer.SandTableViewer";
		public var vid:String = GenerateIDTool.generateID();
		//楼盘
		public static const ESTATE_BUTTON:String = "estate";
		//规划
		public static const LAYOUT_BUTTON:String = "layout";
		//周边
		public static const ENVIRONMENT_BUTTON:String = "envit";
		
		/**
		 * 规划
		 */
		private var layoutPanel:UIComponent = null;
		private var layoutBitmap:Bitmap = null;
		private var mapSelect:Bitmap = null;
		private var mapDefault:Bitmap = null;
		private var mapContainer:UIComponent = null;
		/**
		 * 沙盘
		 */
		private var estateContainer:UIComponent = null;
		private var inEffectMask:UIComponent = null;
		private var outEffectMask:UIComponent = null;
		/**3D沙盘管理*/
		private var service:EstateService;
		/**
		 * 地產項目的庫文件
		 */
		private var estateDepot:SandTableDepot = null;
		private var optionBar:TitleOptionBar = null;
		/**
		 * 判断当前选择的功能
		 */
		private var isEstate:Boolean = true;
		private var isEnvironment:Boolean = false;
		private var isLayout:Boolean = false;
		
		private var depotUrl:String = "resource/sandtable/sandTableDepot.swf";
		
		public function SandTableViewer()
		{
			super();
			this.title = "沙盘演示";
			this.showCloseButton = true;
			this.width = 660;
			this.height = 523;
			this.layout = "absulate";
			this.setStyle("paddingLeft",0);
			this.setStyle("paddingRight",0);
			this.setStyle("paddingTop",0);
			this.setStyle("paddingBottom",0);
			this.addEventListener(CloseEvent.CLOSE,closeHandler);
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.addEventListener(PanelStateChangeEvent.REMOVE,removeHandler);
		}
		
		private function init(event:Event):void{
			//加载共享库
			this.canDragMove = true;
			loadShareDepot();
			Application.application.moveCenter(this);
		}

		private function loadShareDepot():void
		{
			estateDepot = SandTableDepot.getInstance();
			estateDepot.load(depotUrl);
			estateDepot.callFunction(initShareDepot);
		}

		/**
		 *  加载完库文件后，开始初始化
		 *
		 */
		private function initShareDepot():void
		{
			optionBar = new TitleOptionBar();
			this.estateContainer = new UIComponent();
			inEffectMask = new UIComponent();
			outEffectMask = new UIComponent();
			inEffectMask.mouseChildren = false;
			inEffectMask.mouseEnabled = false;
			outEffectMask.mouseChildren = false;
			outEffectMask.mouseEnabled = false;
			inEffectMask.addChild(estateDepot.getMovieClip("InEffectMC"));
			outEffectMask.addChild(estateDepot.getMovieClip("OutEffectMC"));
		
			service = new EstateService("resource/sandtable/data.xml");
			estateContainer.addChild(service.displayContainer);
			estateContainer.addChild(inEffectMask);
			estateContainer.addChild(outEffectMask);
			outEffectMask.alpha = 0;
			estateContainer.x = 5;
			
			layoutBitmap = new Bitmap();
			layoutPanel = new UIComponent();
			mapContainer = new UIComponent();
			mapSelect = new Bitmap();
			mapDefault = new Bitmap();
			
			mapDefault.bitmapData = estateDepot.getBitmapData("MapDefaultData");
			mapSelect.bitmapData = estateDepot.getBitmapData("MapSelectedData");
			mapContainer.addChild(mapDefault);
			mapContainer.x = 580;
			mapContainer.y = 10;
			layoutBitmap.bitmapData = estateDepot.getBitmapData("LayoutBGData");
			layoutPanel.addChild(layoutBitmap);
			layoutPanel.addChild(mapContainer);
			layoutPanel.alpha = 0;
			mapContainer.addEventListener(MouseEvent.CLICK, mapClickHandler);
			layoutPanel.x = 5;
			layoutPanel.y = 2;
			optionBar.x = 429;
			optionBar.y = 437;
			optionBar.setSelectedState(ESTATE_BUTTON);
			optionBar.getSelectedContainer(ENVIRONMENT_BUTTON).addEventListener(MouseEvent.CLICK, enviHandler);
			optionBar.getSelectedContainer(LAYOUT_BUTTON).addEventListener(MouseEvent.CLICK, layoutHandler);
			optionBar.getSelectedContainer(ESTATE_BUTTON).addEventListener(MouseEvent.CLICK, estateHandler);
			this.addChild(estateContainer);
			this.addChild(optionBar);
		}

		/**
		 * 点击真实地图的事件
		 * @param event
		 *
		 */
		private function mapClickHandler(event:MouseEvent):void
		{
			optionBar.clearContainer(mapContainer);
			mapContainer.addChild(mapSelect);
		}

		/**
		 * 点击楼盘按钮的事件
		 * @param event
		 *
		 */
		private function estateHandler(event:MouseEvent):void
		{
			if(!service.estate.configCompleted)return;
			if (!isEstate)
			{
				isEstate = true;
				
				if(!estateContainer.visible){
					estateContainer.visible = true;
				}
				if (isLayout)
				{
					TweenLite.to(layoutPanel, 2, {autoAlpha:0, ease:Expo.easeInOut, onComplete:onFinishTween});
					TweenLite.to(estateContainer, 2, {autoAlpha:1, ease:Exponential.easeInOut});
				}

				service.zoomControl(0.4, 1);
				TweenLite.to(inEffectMask, 2, {autoAlpha:1, ease:Expo.easeInOut});
				TweenLite.to(outEffectMask,2, {autoAlpha:0, ease:Expo.easeInOut});
				isLayout = false;
				isEnvironment = false;
				optionBar.setSelectedState(ESTATE_BUTTON);
			}
		}

		/**
		 * 点击周边地图的事件
		 * @param event
		 *
		 */
		private function enviHandler(event:MouseEvent):void
		{
			
			if(service.estate.configCompleted){
				
			
			if (!isEnvironment)
			{
				isEnvironment = true;
				service.zoomControl(0.2, 2);
				if(!estateContainer.visible){
					estateContainer.visible = true;
				}
				if (isLayout)
				{
					TweenLite.to(layoutPanel, 2, {autoAlpha:0, ease:Expo.easeInOut, onComplete:onFinishTween});
					TweenLite.to(estateContainer, 2, {autoAlpha:1, ease:Expo.easeInOut});
				}
				TweenLite.to(inEffectMask, 2, {autoAlpha:0, ease:Expo.easeInOut});
				TweenLite.to(outEffectMask, 2, {autoAlpha:1, ease:Expo.easeInOut});
				isEstate = false;
				isLayout = false;
				optionBar.setSelectedState(ENVIRONMENT_BUTTON);
				
			}
			}
		}

		/**
		 * 点击规划按钮的事件
		 * @param event
		 *
		 */
		private function layoutHandler(event:MouseEvent):void
		{

			if (!isLayout)
			{
				isEnvironment = false;
				isEstate = false;
				isLayout = true;
				optionBar.setSelectedState(LAYOUT_BUTTON);
				this.addChildAt(layoutPanel, 0);
				TweenLite.to(estateContainer, 2, {autoAlpha:0, ease:Expo.easeInOut, onComplete:onFinishTween});
				TweenLite.to(layoutPanel, 2, {autoAlpha:1, ease:Expo.easeInOut});
			}
		}

		private function onFinishTween():void
		{

		if (isEnvironment)
			{
			
				if (this.contains(layoutPanel))
				{
					this.removeChild(layoutPanel);
				}
			}
			else if (isEstate)
			{

				if (this.contains(layoutPanel))
				{
					this.removeChild(layoutPanel);
				}
				
			}
			else  if (isLayout)
			{
				estateContainer.visible = false;
			}
		}
		
		private function closeHandler(event:Event):void
		{
			var uiController:UIController = ObjectCollection.beansContext.getObject("uiController") as UIController;
			uiController.removedFromDesktop(this);
		}

		private function removeHandler(e:Event):void
		{
			this.removeEventListener(CloseEvent.CLOSE,closeHandler);
			this.removeEventListener(FlexEvent.CREATION_COMPLETE,init);
			this.removeEventListener(PanelStateChangeEvent.REMOVE,removeHandler);
			dispose();
		}
		
		public override function dispose():void
		{
			super.dispose();
			service.dispose();
			optionBar.clearBitmap(mapDefault);
			optionBar.clearBitmap(mapSelect);
			optionBar.clearContainer(mapContainer);
			optionBar.clearContainer(layoutPanel);
			optionBar.clearBitmap(layoutBitmap);
			mapContainer.removeEventListener(MouseEvent.CLICK, mapClickHandler);
			optionBar.getSelectedContainer(ENVIRONMENT_BUTTON).removeEventListener(MouseEvent.CLICK, enviHandler);
			optionBar.getSelectedContainer(LAYOUT_BUTTON).removeEventListener(MouseEvent.CLICK, layoutHandler);
			optionBar.getSelectedContainer(ESTATE_BUTTON).removeEventListener(MouseEvent.CLICK, estateHandler);
			optionBar.clear();
			optionBar = null;
			mapDefault = null;
			mapSelect = null;
			mapContainer = null;
			layoutBitmap = null;
			layoutPanel = null;
			service = null;
		}
		
	}
}